﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.ColorSpaces;
using LowLevelGraphics.Types.Interval;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is a color filter
    /// It works like the rgb color filter, except
    /// the L, u, v values are filtered instead of R G B
    /// </summary>
    public class IRgByFilter : BaseForeBackgroundColorFilter
    {
        protected DoubleInterval m_IInterval = new DoubleInterval(0.0f, 1.0f);
        protected DoubleInterval m_RgInterval = new DoubleInterval(0.0f, 1.0f);
        protected DoubleInterval m_ByInterval = new DoubleInterval(0.0f, 1.0f);

        /// <summary>
        /// Default constructor
        /// </summary>
        public IRgByFilter()
        {
        }

        /// <summary>
        /// Primary constructor to specify intervals directly
        /// </summary>
        /// <param name="_i">The _i.</param>
        /// <param name="_rg">The _RG.</param>
        /// <param name="_by">The _by.</param>
        public IRgByFilter(DoubleInterval _i, DoubleInterval _rg, DoubleInterval _by)
        {
            m_IInterval = _i;
            m_RgInterval = _rg;
            m_ByInterval = _by;
        }

        /// <summary>
        /// Used by clone to create a deep copy
        /// </summary>
        /// <param name="_irgbyFilter"></param>
        internal IRgByFilter(IRgByFilter _irgbyFilter)
        {
            this.m_IInterval = _irgbyFilter.m_IInterval;
            this.m_RgInterval = _irgbyFilter.m_RgInterval;
            this.m_ByInterval = _irgbyFilter.m_ByInterval;
        }

        /// <summary>
        /// Gets or sets the I.
        /// </summary>
        /// <value>The I.</value>
        public DoubleInterval I
        {
            get { return m_IInterval; }
            set { m_IInterval = value; }
        }

        /// <summary>
        /// Gets or sets the rg.
        /// </summary>
        /// <value>The rg.</value>
        public DoubleInterval Rg
        {
            get { return m_RgInterval; }
            set { m_RgInterval = value; }
        }

        /// <summary>
        /// Gets or sets the by.
        /// </summary>
        /// <value>The by.</value>
        public DoubleInterval By
        {
            get { return m_ByInterval; }
            set { m_ByInterval = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            IRgBy hsb = null;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    hsb = new IRgBy(bitmap.GetPixel(x,y));

                    if (m_IInterval.Contains(hsb.I)
                        && m_RgInterval.Contains(hsb.Rg)
                        && m_ByInterval.Contains(hsb.By))
                    {
                        bitmap.SetPixel(x, y, hsb.ToColor());

                    }
                    else
                    {
                        bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new IRgByFilter(this);
        }        
    }
}
